﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq;
using System.Xml;
using BLL = KC.Business.BlogSlinger;
using KC.DataAccess.BlogSlinger;

public partial class rss : System.Web.UI.Page
{
	private string cs = System.Configuration.ConfigurationManager.ConnectionStrings["SQL"].ConnectionString;

	protected void Page_Load(object sender, EventArgs e)
	{
		BlogSlingerDataContext dc = new BlogSlingerDataContext(cs);
		int BlogID = -1;
		if (Request.QueryString["ID"] != null && !string.IsNullOrEmpty(Request.QueryString["ID"]))
			int.TryParse(Request.QueryString["ID"], out BlogID);
		Response.Clear();
		Response.ContentType = "application/rss+xml";

		Blog blog = BlogID == -1 ? null : BLL.GetBlog(ref dc, BlogID);
		XmlDocument xDoc = BuildRSS(ref dc, blog);
		if (xDoc == null) return;


		Response.Write(xDoc.OuterXml);
		Response.Flush();
		Response.End();
	}

	private XmlDocument BuildRSS(ref BlogSlingerDataContext  dc, Blog blog)
	{
		XmlDocument xDoc = BuildHeader(blog);
		var posts = blog == null
			? BLL.GetRecentPosts(ref dc)
			: BLL.GetPostsByBlog(ref dc, blog.ID, 10);

		XmlNode xe = xDoc.SelectSingleNode(@"rss/channel");
		foreach (Post post in posts)
			xe.AppendChild(BuildPostRssElement(ref dc, ref xDoc, post));

		return xDoc;
	}

	private XmlDocument BuildHeader(Blog blog)
	{
		XmlDocument xDoc = new XmlDocument();
		XmlElement root = xDoc.CreateElement("rss");
		root.Attributes.Append(xDoc.CreateAttribute("version"));
		root.Attributes["version"].InnerText = "2.0";

		XmlElement
			channel = xDoc.CreateElement("channel"),
			title = xDoc.CreateElement("title"),
			link = xDoc.CreateElement("link"),
			description = xDoc.CreateElement("description");

		title.InnerText = "CSBN - " + (blog == null ? "All Blogs" : blog.Name);
		description.InnerText = blog == null ? "All items from all blogs at the Code-Slinger Blog Network" : (blog.Description ?? blog.Name ?? "");
		link.InnerText = "http://blogs.code-slinger.com/rss.aspx" + (blog == null ? "" : "ID=" + blog.ID.ToString());

		channel.AppendChild(title);
		channel.AppendChild(link);
		channel.AppendChild(description);
		root.AppendChild(channel);
		xDoc.AppendChild(root);
		return xDoc;
	}
	private XmlElement BuildPostRssElement(ref BlogSlingerDataContext dc, ref XmlDocument xDoc, Post post)
	{
		XmlElement
			item = xDoc.CreateElement("item"),
			title = xDoc.CreateElement("title"),
			description = xDoc.CreateElement("description"),
			link = xDoc.CreateElement("link"),
			pubdate = xDoc.CreateElement("pubDate");
		title.InnerText = post.Title;
		description.InnerText = post.Excerpt;
		link.InnerText = "http://blogs.code-slinger.com/Post.aspx?Title=" + post.FriendlyName;
		pubdate.InnerText = string.Format("{0:R}", post.PublishDate);

		item.AppendChild(title);
		item.AppendChild(description);
		item.AppendChild(link);
		item.AppendChild(pubdate);
		return item;
	}

}